﻿using DSPL.Contracts;
using DSPL.Contracts.Interfaces;
using DSPL.DataAccess;
using System.Collections.Generic;
using System;
using DSPL.Contracts.Common;


namespace DSPL.Services.Services
{
    public class CommonReadService : ICommonReadService
    {
        public int GetUserWiseCostCenter()
        {
            int costCenter = 0;

            var db = DatabaseAccess.Instance;
            List<ColumnWithValue> columnList = new List<ColumnWithValue>();
            columnList.Add(new ColumnWithValue() { ColumnName = "DCostPk" });
           

            List<ColumnWithValue> whereList = new List<ColumnWithValue>();
            whereList.Add(new ColumnWithValue() { ColumnName = "UserId", Comparison = QComparison.EQUALS, ColumnValue = GlobalVariables.LoggedInUser });
            
            Query query = new Query() { TableName = "FUsrCosHed", ColValues = columnList.ToArray(), QType = QueryType.SELECT, WhereClause = whereList.ToArray() };
            var  dtCost = db.ExcecuteSelect(query);

            if (dtCost.Rows.Count > 0)
            {
               costCenter = Convert.ToInt32(dtCost.Rows[0]["DCostPk"]);
            }
            
            return costCenter;
        }


        public int GetUserWiseLocation()
        {
            int location = 0;
            var db = DatabaseAccess.Instance;
            List<ColumnWithValue> columnList = new List<ColumnWithValue>();
            columnList.Add(new ColumnWithValue() { ColumnName = "DLocationPk" });


            List<ColumnWithValue> whereList = new List<ColumnWithValue>();
            whereList.Add(new ColumnWithValue() { ColumnName = "UserId", Comparison = QComparison.EQUALS, ColumnValue = GlobalVariables.LoggedInUser });

            Query query = new Query() { TableName = "FUsrLocHed", ColValues = columnList.ToArray(), QType = QueryType.SELECT, WhereClause = whereList.ToArray() };
            var dtLocation = db.ExcecuteSelect(query);

            if (dtLocation.Rows.Count > 0)
            {
                location = Convert.ToInt32(dtLocation.Rows[0]["DLocationPk"]);
            }
            return location;
        }
    }
}
